package com.jash.alipaytest;

import android.app.Activity;
import android.os.AsyncTask;
import android.support.annotation.Nullable;

import com.alipay.sdk.app.PayTask;

import java.util.Map;


public class AsyncPayTask extends AsyncTask<String, Void, String> {
    private Activity context;
    private Callback callback;

    public AsyncPayTask(Activity context) {
        this.context = context;
        if (context instanceof Callback) {
            callback = (Callback) context;
        }
    }

    @Override
    protected String doInBackground(String... params) {
        PayTask alipay = new PayTask(context);
        Map<String, String> result = alipay.payV2(params[0],true);
        String res = null;
        switch (Integer.parseInt(result.get("resultStatus"))) {
            case 9000:
                break;
            case 8000:
                res = "正在处理中，支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
                break;
            case 4000:
                res = "订单支付失败";
                break;
            case 5000:
                res = "重复请求";
                break;
            case 6001:
                res = "用户中途取消";
                break;
            case 6002:
                res = "网络连接出错";
                break;
            case 6004:
                res = "支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
                break;
            default:
                res = "其它支付错误";
                break;
        }
        return res;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        callback.callback(s);
    }

    public interface Callback {
        void callback(@Nullable String error);
    }
}
